package com.qst.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qst.order.domain.DTO.OrderItemDTO;
import com.qst.order.domain.DTO.TopProductDTO;
import com.qst.order.domain.DTO.TrendItem;
import com.qst.order.domain.OrderItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.domain.Page;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderItemMapper extends BaseMapper<OrderItem> {


    List<OrderItemDTO> selectOrderItemDTOByDateRange(
            @Param("start") LocalDate  start,
            @Param("end") LocalDate  end,
            @Param("merchantId") Integer merchantId
    );

    // 统计指定时间和商家的总销售额
    @Select("SELECT COALESCE(SUM(total_price), 0) FROM t_order_item " +
            "WHERE create_time BETWEEN #{start} AND #{end} " +
            "AND merchant_id = #{merchantId} AND is_deleted = 0")
    BigDecimal sumTotalPriceByDateAndMerchant(
            @Param("start") LocalDate  start,
            @Param("end") LocalDate  end,
            @Param("merchantId") Integer merchantId);

    // 统计指定时间和商家的商品销售总量
    @Select("SELECT COALESCE(SUM(quantity), 0) FROM t_order_item " +
            "WHERE create_time BETWEEN #{start} AND #{end} " +
            "AND merchant_id = #{merchantId} AND is_deleted = 0")
    Integer sumQuantityByDateAndMerchant(
            @Param("start") LocalDate  start,
            @Param("end") LocalDate end,
            @Param("merchantId") Integer merchantId);

    List<TrendItem> selectTrendData(LocalDate  start, LocalDate  end, Integer merchantId, String groupFormat);

    List<TopProductDTO> selectTopProducts(LocalDate  start, LocalDate  end, Integer merchantId, Integer topN);

    void batchUpdateStatus(@Param("orderItemNos") List<String> orderItemNo,@Param("status") String refunded);
}
